<script setup>
	import { ref } from 'vue'
	import { patientListApi, removePatientApi } from '@/services/patient.js'
	import { onShow } from '@dcloudio/uni-app'
	const deleteOpt = [
		{
			text: '删除',
			style: {
				backgroundColor: '#dd524d',
			},
		},
	]
	const pageShow = ref(false)
	const isOpened = ref(true)
	const patients = ref([])
	async function onSwipeActionClick(id, index) {
		const { code, message } = await removePatientApi(id)
		if (code !== 10000) return uni.utils.toast(message)
		patients.value.splice(index, 1)
	}
	async function getPatientList() {
		const { code, message, data } = await patientListApi()
		if (code !== 10000) return uni.utils.toast('列表获取失败，稍后重试!')
		patients.value = data
		pageShow.value = true
	}
	onShow(() => {
		getPatientList()
	})
</script>
<template>
	<scroll-page>
		<!-- 防抖 -->
		<view class="archive-page" v-if="pageShow">
			<view class="archive-tips"> 最多可添加6人 </view>
			<uni-swipe-action>
				<uni-swipe-action-item
					:right-options="deleteOpt"
					@click="onSwipeActionClick(item.id, index)"
					v-for="(item, index) in patients"
					:key="item.id"
				>
					<view class="archive-card" :class="{ active: true }">
						<view class="archive-info">
							<text class="name">{{ item.name }}</text>
							<text class="id-card">{{
								item.idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1******$2')
							}}</text>
							<text class="default" v-if="item.defaultFlag === 1">默认</text>
						</view>
						<view class="archive-info">
							<text class="gender">{{ item.genderValue }}</text>
							<text class="age">{{ item.age }}岁</text>
						</view>
						<navigator hover-class="none" :url="'/subpkg_archive/form/index?id='+item.id" class="edit-link">
							<uni-icons
								type="icon-edit"
								size="26"
								color="#20c6b2"
								custom-prefix="iconfont"
							></uni-icons>
						</navigator>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>

			<view class="archive-card" v-if="patients.length < 6">
				<navigator hover-class="none" url="/subpkg_archive/form/index">
					<view class="add-link">
						<uni-icons type="plusempty" size="26" color="#20c6b2"></uni-icons>
						<text class="label">添加患者</text>
					</view>
				</navigator>
			</view>
		</view>
	</scroll-page>
</template>

<style lang="scss" scoped>
	.archive-page {
		padding: 30rpx;
		.archive-tips {
			line-height: 1;
			padding-left: 10rpx;
			margin: 30rpx 0;
			color: #6f6f6f;
			font-size: 26rpx;
		}
		.archive-card {
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			position: relative;

			height: 180rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;
			border-radius: 10rpx;

			box-sizing: border-box;
			border: 1rpx solid transparent;
			background-color: #f6f6f6;
			&.active {
				background-color: rgba(44, 181, 165, 0.1);
				.default {
					display: block;
				}
			}
			.archive-info {
				display: flex;
				align-items: center;
				color: #6f6f6f;
				font-size: 28rpx;
				margin-bottom: 10rpx;
				.name {
					margin-right: 30rpx;
					color: #121826;
					font-size: 32rpx;
					font-weight: 500;
				}
				.id-card {
					color: #121826;
				}
				.gender {
					margin-right: 30rpx;
				}
				.default {
					// display: none;
					height: 36rpx;
					line-height: 36rpx;
					text-align: center;
					padding: 0 12rpx;
					margin-left: 30rpx;
					border-radius: 4rpx;
					color: #fff;
					font-size: 24rpx;
					background-color: #16c2a3;
				}
			}
			.edit-link {
				position: absolute;
				top: 50%;
				right: 30rpx;
				transform: translateY(-50%);
			}
		}
		.add-link {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			.label {
				margin-top: 10rpx;
				font-size: 28rpx;
				color: #16c2a3;
			}
		}
	}
	:deep(.uni-swipe_button-group) {
		bottom: 30rpx;
	}
</style>